package com.hspedu.newData.dmsxl.动态规划.基础;

import org.junit.Test;

/**
 * @Author: yh
 * @description:
 * @CreateTime: 2025-05-06
 * @Version: 1.0
 */

public class LC416分割等和子集 {
    public boolean canPartition(int[] nums) {
        int len = nums.length;
        int target = 0;
        for (int i = 0; i < len; i++) {
            target += nums[i];
        }
        if (target % 2 != 0) return false;
        target /= 2;

        boolean[][] dp = new boolean[len][target+1];

        dp[0][nums[0]] = true;
        
        for (int i = 1; i < len; i++) {
            for (int j = 0; j <= target; j++) {
                dp[i][j] = dp[i-1][j];

                if (nums[i] == j){
                    dp[i][j] = true;
                    continue;
                }
                if (nums[i] < j){
                    dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]];
                }
            }
        }
        return dp[len-1][target];
    }

    @Test
    public void test() {
        // 测试代码
        canPartition(new int[]{1,5,11,5});
        System.out.println("------");

    }

}
